//
//  TrainStation.m
//  MINFOBBS
//
//  Created by juvid on 13-11-28.
//  Copyright (c) 2013年 minfo. All rights reserved.
//

#import "TrainStation.h"
#import "UIColor+expanded.h"
#import "DetectionSystemVersion.h"
#import "MainSearchViewController.h"
#import "SBJson.h"
@interface TrainStation ()

@end

@implementation TrainStation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self BackButton];
    if ([[DetectionSystemVersion alloc]Version]) {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    if (self.isQuery) {
        [self GetDatas];
        self.labTrainClass.text=self.strClass;
        self.imgTrainClass.image=[UIImage imageNamed:self.strClass];
    }
    else{
    if (self.isSelf) {
        self.vieOUt.hidden=NO;
    }
        [self ShowData];
    }
//    }
    // Do any additional setup after loading the view from its nib.
}
-(void)ShowData{
    NSArray *arr=[[self.arrReturn objectForKey:@"Properties"] objectForKey:@"Property"];
    
    self.labTimes.text=[[arr objectAtIndex:arr.count-3] objectForKey:@"text"];
    //        self.labTrainClass.text=[[arr objectAtIndex:1] objectForKey:@"text"];
    //self.imgTrainClass.image=[UIImage imageNamed:[[arr objectAtIndex:arr.count-5] objectForKey:@"text"]];
    self.labFirst.text=[[arr objectAtIndex:arr.count-5] objectForKey:@"text"];
    self.labEnd.text=[[arr objectAtIndex:arr.count-4] objectForKey:@"text"];
    self.table.separatorStyle=UITableViewCellSeparatorStyleNone;
    arrTick=[[NSMutableArray alloc]init];
    for (int i=0; i<arr.count-5; i++) {
        if (![[[arr objectAtIndex:i] objectForKey:@"text"] isEqual:@""]) {
            [arrTick addObject:[arr objectAtIndex:i]];
        }
    }
    //self.arrReturn=nil;

}
-(void)viewDidAppear:(BOOL)animated{
    if (!self.isSelf) {
         self.vieOUt.hidden=NO;
        self.vieOUt.frame=CGRectMake(self.vieOUt.frame.origin.x, 12, self.vieOUt.frame.size.width, self.view.frame.size.height-24);
        self.vieOUt.layer.cornerRadius=10;
        self.labTimes.font=[UIFont systemFontOfSize:25];
        self.labTimes.textAlignment=NSTextAlignmentLeft;
        self.labFirst.font=[UIFont systemFontOfSize:16];
        self.labFirst.frame=CGRectMake(self.labFirst.frame.origin.x, self.labFirst.frame.origin.y +8 , self.labFirst.frame.size.width, self.labFirst.frame.size.height);
        self.imgForm.frame= CGRectMake(self.imgForm.frame.origin.x, self.imgForm.frame.origin.y +8 , self.imgForm.frame.size.width, self.imgForm.frame.size.height);
        self.labEnd.font=[UIFont systemFontOfSize:16];
        self.labEnd.frame=CGRectMake(self.labEnd.frame.origin.x, self.labEnd.frame.origin.y +8 , self.labEnd.frame.size.width, self.labEnd.frame.size.height);
        self.vieSecond.frame=CGRectMake(self.vieSecond.frame.origin.x, self.vieSecond.frame.origin.y  +8, self.vieSecond.frame.size.width, self.vieSecond.frame.size.height+10);
         self.table.frame=CGRectMake(self.table.frame.origin.x+10, self.table.frame.origin.y +18 , self.table.frame.size.width-20, self.table.frame.size.height-30);
    }
}
-(void)BackButton{
    self.navigationItem.titleView=[DetectionSystemVersion SetBarTitle:@"百事搜"];
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btnBack=[[DetectionSystemVersion alloc]BackButton];
    [btnBack addTarget:self action:@selector(BackReturn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem=backItem;
}
- (void)viewWillDisappear:(BOOL)animated{
    [tRequest clearDelegatesAndCancel];
    [tRequest cancel];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [NSThread cancelPreviousPerformRequestsWithTarget:self];
}
#pragma mark 异步数据请求
-(void)GetDatas{
    tRequest=[[[ScreenAccess alloc]init]ASIGetSearch:self.strKey Start:0  Limit:4];
    [tRequest setDelegate:self];
    [tRequest setUsername:@"allMessage"];
    
}
-(void)requestFinished:(ASIHTTPRequest*)request{
    
    NSString *response = [request responseString];
    SBJsonParser * parser = [[SBJsonParser alloc] init];
    NSMutableDictionary *Result = [parser objectWithString:response];
    
    if ([request.username isEqual:@"allMessage"]){
        BOOL IsSuccess=[[[Result objectForKey:@"Response"]objectForKey:@"status"]integerValue]==200?YES:NO;
        if (IsSuccess==YES) {
            if (self.arrReturn==nil) {
                self.arrReturn=[[NSMutableDictionary alloc]init];
            }
            self.arrReturn =[[[[[[[Result objectForKey:@"Response"] objectForKey:@"Categories"] objectForKey:@"Category"] objectAtIndex:0]objectForKey:@"Items"] objectForKey:@"Item"]objectAtIndex:0];
            [self ShowData];
            [self.table reloadData];
            
        }
    }
}

-(void)requestFailed:(ASIHTTPRequest*)request{
    
    
}

#pragma mark 导航栏按钮事件
-(void)BackReturn:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (self.isSelf) {
        return 2;
    }
    return arrTick.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.backgroundColor=[UIColor clearColor];
    if (indexPath.row%2==1) {
        UIView *vie=[[UIView alloc]init];
        vie.backgroundColor=[UIColor colorWithHexString:@"0xf3f3f3"];
        cell.backgroundView=vie;
    }
    else{
        cell.backgroundColor=[UIColor whiteColor];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.textLabel.text=[[arrTick objectAtIndex:indexPath.row] objectForKey:@"name"];
    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 44)];
    lab.text=[[arrTick objectAtIndex:indexPath.row] objectForKey:@"text"];
    lab.textColor=[UIColor colorWithHexString:@"0x565656"];
    lab.font=[UIFont systemFontOfSize:14];
    lab.backgroundColor=[UIColor clearColor];
    cell.accessoryView=lab;
    cell.textLabel.font=[UIFont systemFontOfSize:14];
    cell.indentationLevel = 1;
    cell.indentationWidth = 20.0f;
    cell.textLabel.textColor=[UIColor colorWithHexString:@"0x565656"];
    //    cell.textLabel.text=@"string";
    // Configure the cell...
    
    return cell;
}
-(float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.isSelf) {
        return 38;
    }
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *result;
    result=[[UIView alloc]init];
    result.backgroundColor=[UIColor clearColor];
    if (self.isSelf) {
        UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.table.frame.size.width, 3)];
        img.image=[UIImage imageNamed:@"footLine"];
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 3, self.table.frame.size.width, 35)];
        [btn setTitle:@"点击查看所有站点" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(PrseeMore) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:[UIColor colorWithHexString:@"0xb7b7b7"] forState:UIControlStateNormal];
        btn.titleLabel.font=[UIFont systemFontOfSize:14];
        [result addSubview:btn];
        [result addSubview:img];
    }
    return result;
}
- (MainSearchViewController *)menuContainerViewController {
    return (MainSearchViewController *)self.parentViewController;
}
-(void)PrseeMore{
    //    if (self.isSelf) {
    TrainStation *jokecon=[[TrainStation alloc]init];
    jokecon.arrReturn=self.arrReturn;
    [ [self menuContainerViewController].navigationController  pushViewController:jokecon animated:YES];
    //    }
    
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
